<?php

/**
 * kpiday actions.
 *
 * @package    seth
 * @subpackage kpiday
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class kpidayActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      
      $this->range=$request->getParameter('range', 'day');
      $date=$request->getParameter('date', NULL);
      
      switch ($this->range):
          case "day":
              if (isset($date))
                  $start=new DateTime($date);
              else 
                  $start=new DateTime();
              $end=$start;
              $partial="";
              break;
          case "month":
              $start=new DateTime();
              $start->sub(new DateInterval("P14D"));
              $end=new DateTime();
              $partial="Many";
              break;
          case "year":
              $start=new DateTime();
              $end=new DateTime();
              $partial="";
              break;
      endswitch;
      
      $this->start=$start;
      $this->end=$end;
      $this->partial=$partial;
            
      $plants=Doctrine_Core::getTable('Plant')->GetPlantIndex(array(
          "orderBy"=>"name",
          "indexOrder"=>"ASC"));
      
      $this->kpidays=array();
      foreach ($plants as $plant){
         $this->kpidays[$plant->getId()]= Doctrine_Core::getTable('kpiday')->getKpiDayByPlant($plant, array("start"=>$this->start, "end"=>$this->end,));
      }
      
      if ($request->isXmlHttpRequest()){
        return $this->renderPartial('kpiday/list'.$partial, array("kpidays"=>$this->kpidays,"start"=>$start,"end"=>$end));
      }

  }

  public function executeShow(sfWebRequest $request)
  {
    //$kpiday = $this->getRoute()->getObject();
    $this->plant=$this->getRoute()->getObject();
    $this->PlantKpidays=$this->plant->getKpiDay();
    
    $date=new DateTime($request->getParameter('date', "2013-04-21"));
    $this->srcDatas=$this->getMainSourceData($this->plant, $date );
    
    
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new kpidayForm();
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new kpidayForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
    
    
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->form = new kpidayForm($this->getRoute()->getObject());
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->form = new kpidayForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->getRoute()->getObject()->delete();

    $this->redirect('kpiday/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $kpiday = $form->save();

      $this->redirect('kpiday/edit?id='.$kpiday->getId());
    }
  }
  
  private function getMainSourceData(Plant $plant, Datetime $day){
      
      $meters=$plant->getMonitoredPowerDev(array("AcqSyst"=>"all","type"=>"Energy Meter","subtype"=>"GSE"));   
      $weaths=$plant->getMonitoredPowerDev(array("AcqSyst"=>"all","type"=>"Weather"));
      
      //get times
                foreach ($meters as $dev){
                     $devLastUpdate=$dev->getLastUpdate($day);
                     if (isset($devLastUpdate))
                        if (!isset($KpiLastUpdate) or ($KpiLastUpdate>$devLastUpdate))
                            $KpiLastUpdate=new DateTime($devLastUpdate->format("Y-m-d H:i:s"));  
                }
                //check Weather Sensors
                foreach ($weaths as $dev){
                     $devLastUpdate=$dev->getLastUpdate($day);
                     if (isset($devLastUpdate))
                        if (!isset($KpiLastUpdate) or ($KpiLastUpdate>$devLastUpdate))
                            $KpiLastUpdate=new DateTime($devLastUpdate->format("Y-m-d H:i:s"));  
                }
                
                if (!isset($KpiLastUpdate))
                    $KpiLastUpdate=new DateTime($day->format("Y-m-d")." 23:45");
                 
                $to=$KpiLastUpdate;
                $from=new DateTime($to->format("Y-m-d")." 00:00");
      
      $vars=array("sampletime, actexpen");
      foreach($meters as $meter){
          $res=$meter->getVar(array("vars"=>$vars,"from"=>$from,"to"=>$to,"noOrder"=>True,"returnArray"=>True));
          foreach($res as $key => $row){
              $prevKey=($key-1)>=0?($key-1):0;
              $srcDatas[$meter->getLabel()][]=array("sampletime"=>$row["sampletime"], "Pac/Pn"=>( ($row["actexpen"]-$res[$prevKey]["actexpen"])/$plant->getPn()));
          }
          
      }
            
      $vars=array("sampletime, irr");
      foreach($weaths as $weath){
          
          $res=$weath->getVar(array("vars"=>$vars,"from"=>$from,"to"=>$to,"noOrder"=>True,"returnArray"=>True));
          foreach($res as $key => $row){
              $srcDatas[$weath->getLabel()][]=array("sampletime"=>$row["sampletime"], "Irr/Irr STC"=>( ($row["irr"])/1000));
          }
          
      }
      return $srcDatas;
     
  }
 
}
